package com.xiaokaceng.openci.dto;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import com.xiaokaceng.openci.domain.Developer;
import com.xiaokaceng.openci.domain.Project;
import com.xiaokaceng.openci.domain.ProjectDeveloper;
import com.xiaokaceng.openci.domain.Tool;
import com.xiaokaceng.openci.domain.ToolConfiguration;

public class ProjectListDto extends BaseDto {

	private static final long serialVersionUID = -8867506934224022832L;

	private Long id;

	private String name;

	private String projectStatus;

	private String projectCreateDate;

	private List<DeveloperDto> developerDtos;

	private List<ToolConfigurationDto> configurationDtos;

	public static List<ProjectListDto> transformBy(List<Project> projects) {
		List<ProjectListDto> projectListDtos = new ArrayList<ProjectListDto>();
		if (!projects.isEmpty()) {
			for (Project each : projects) {
				ProjectListDto projectListDto = new ProjectListDto();
				projectListDto.setId(each.getId());
				projectListDto.setName(each.getName());
				projectListDto.setProjectStatus(each.getProjectStatus().toString());
				projectListDto.setProjectCreateDate(dateToString(each.getProjectCreateDate()));
				projectListDto.setDeveloperDtos(createDeveloperDtos(each.getDevelopers()));
				projectListDto.setConfigurationDtos(createConfigurationDtos(each));
				projectListDtos.add(projectListDto);
			}
		}
		return projectListDtos;
	}

	private static List<ToolConfigurationDto> createConfigurationDtos(Project project) {
		List<ToolConfigurationDto> toolConfigurationDtos = new ArrayList<ToolConfigurationDto>();
		for (Tool each : project.getTools()) {
			ToolConfiguration toolConfiguration = each.getToolConfiguration();
			if (toolConfiguration != null) {
				ToolConfigurationDto toolConfigurationDto = new ToolConfigurationDto();
				toolConfigurationDto.setId(toolConfiguration.getId());
				toolConfigurationDto.setName(toolConfiguration.getName());
				toolConfigurationDto.setRequestUrl(toolConfiguration.getRequestAddress(project.getName()));
				toolConfigurationDto.setToolType(toolConfiguration.getToolType().toString());
				toolConfigurationDtos.add(toolConfigurationDto);
			}
		}
		return toolConfigurationDtos;
	}

	private static List<DeveloperDto> createDeveloperDtos(Set<ProjectDeveloper> developers) {
		List<DeveloperDto> developerDtos = new ArrayList<DeveloperDto>();
		for (ProjectDeveloper each : developers) {
			Developer developer = each.getDeveloper();
			DeveloperDto developerDto = new DeveloperDto(developer.getId(), developer.getDeveloperId(), developer.getName(), developer.getEmail(), developer.getPassword());
			developerDtos.add(developerDto);
		}
		return developerDtos;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getProjectStatus() {
		return projectStatus;
	}

	public void setProjectStatus(String projectStatus) {
		this.projectStatus = projectStatus;
	}

	public String getProjectCreateDate() {
		return projectCreateDate;
	}

	public void setProjectCreateDate(String projectCreateDate) {
		this.projectCreateDate = projectCreateDate;
	}

	public List<DeveloperDto> getDeveloperDtos() {
		return developerDtos;
	}

	public void setDeveloperDtos(List<DeveloperDto> developerDtos) {
		this.developerDtos = developerDtos;
	}

	public List<ToolConfigurationDto> getConfigurationDtos() {
		return configurationDtos;
	}

	public void setConfigurationDtos(List<ToolConfigurationDto> configurationDtos) {
		this.configurationDtos = configurationDtos;
	}

}
